package com.situ.mall.controller;


import com.situ.imall.util.JSONResult;
import com.situ.imall.util.LayUiTableDataUtil;
import com.situ.mall.pojo.User;
import com.situ.mall.service.impl.UserServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author 周轩霆
 * @Date 2022/8/13 10:56
 **/

@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    @RequestMapping("/login")
    public JSONResult login(@RequestParam Map params, HttpSession session) {

        return userService.login(params, session);

    }

    @RequestMapping("/register")
    public JSONResult register(@RequestParam Map params, HttpSession session) {

        return userService.register(params, session);

    }


    @RequestMapping("/selectAll")
    public List selectAll() {
        return userService.selectAll();
    }

    @RequestMapping("/page")
    public LayUiTableDataUtil selectByPage(@RequestParam Map params) {
        //获取前台分页数据
        String page = (String) params.get("page");
        String limit = (String) params.get("limit");
        LayUiTableDataUtil dataResult = userService.selectByPage(Integer.parseInt(page), Integer.parseInt(limit));
        return dataResult;
    }

    @RequestMapping("/add")
    public JSONResult add(@RequestParam Map params) {

        //添加操作
        JSONResult jsonResult = userService.addUser(params);
        return jsonResult;
    }

    /**
     * 删除操作
     */
    @RequestMapping("/delete")
    public LayUiTableDataUtil deleteById(@RequestParam("id") int id) {
        LayUiTableDataUtil deleteUser = userService.deleteUser(id);
        return deleteUser;
    }

    @RequestMapping("/deleteAll")
    public JSONResult deleteAll(@RequestParam("idArray[]") String[] idArray) {

        return userService.deleteAll(idArray);

    }

    /**
     * 修改相关操作
     */
    @RequestMapping("/selectUserById")
    public JSONResult selectUserById(@RequestParam("id") int id) {
        User user = userService.queryUserById(id);
        return JSONResult.ok(user);

    }

    @RequestMapping("/update")
    public JSONResult updateUserById(@RequestParam Map params) {
        JSONResult jsonResult = userService.updateUserById(params);
        return JSONResult.ok("修改成功", jsonResult);

    }
}
